package com.snj.visangedu.studytime;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.snj.visangedu.R;


public class StopwatchAdapter extends BaseAdapter {
	
	String tag = "StopwatchAdapter";
	
	String packagename = "";
	Context context;
	int layout;
	
	
	ArrayList<StopwatchItem> mRecordList;
	
	LayoutInflater mInflater = null;
	
	public StopwatchAdapter(Context _context, int _layout, ArrayList<StopwatchItem> _newslist, String pkgname) {
		// TODO Auto-generated constructor stub
		
		this.context = _context;
		this.layout = _layout;
		this.mRecordList = _newslist;
		
	}
	
	public void Insert(ArrayList<StopwatchItem> mList)
	{
		mRecordList.addAll(mList);
	}
	
	public void SetList(ArrayList<StopwatchItem> mList)
	{
		mRecordList = mList;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mRecordList.size();
	}

	public StopwatchItem getItem(int position) {
		// TODO Auto-generated method stub
		return mRecordList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
	
		StopwatchHolder holder = null;
		
		int no = 0;
		long rh = 0, rm = 0, rs = 0, rms = 0;
		long eh = 0, em = 0, es = 0, ems = 0;
		
		
		if(convertView == null)
		{
			mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(layout, parent, false);
			
			holder = new StopwatchHolder();
			
			holder.no10 = (ImageView)convertView.findViewById(R.id.img_no10);
			holder.no1 = (ImageView)convertView.findViewById(R.id.img_no1);
			
			holder.r_h10 = (ImageView)convertView.findViewById(R.id.img_r_h10);
			holder.r_h1 = (ImageView)convertView.findViewById(R.id.img_r_h1);
			
			holder.r_m10 = (ImageView)convertView.findViewById(R.id.img_r_m10);
			holder.r_m1 = (ImageView)convertView.findViewById(R.id.img_r_m1);
			
			holder.r_s10 = (ImageView)convertView.findViewById(R.id.img_r_s10);
			holder.r_s1 = (ImageView)convertView.findViewById(R.id.img_r_s1);
			
			holder.r_ms10 = (ImageView)convertView.findViewById(R.id.img_r_ms10);
			holder.r_ms1 = (ImageView)convertView.findViewById(R.id.img_r_ms1);
			
			holder.e_h10 = (ImageView)convertView.findViewById(R.id.img_e_h10);
			holder.e_h1 = (ImageView)convertView.findViewById(R.id.img_e_h1);
			
			holder.e_m10 = (ImageView)convertView.findViewById(R.id.img_e_m10);
			holder.e_m1 = (ImageView)convertView.findViewById(R.id.img_e_m1);
			
			holder.e_s10 = (ImageView)convertView.findViewById(R.id.img_e_s10);
			holder.e_s1 = (ImageView)convertView.findViewById(R.id.img_e_s1);
			
			holder.e_ms10 = (ImageView)convertView.findViewById(R.id.img_e_ms10);
			holder.e_ms1 = (ImageView)convertView.findViewById(R.id.img_e_ms1);
			
			convertView.setTag(holder);
			
			
		}
		else
		{
			holder = (StopwatchHolder)convertView.getTag();
		}
		
		no = mRecordList.get(position).GetNo();
		
		holder.no10.setVisibility(no < 10 ? View.GONE : View.VISIBLE);
		holder.no10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(no / 10)) + "_b", "drawable", context.getPackageName()));
		holder.no1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(no % 10)) + "_b", "drawable", context.getPackageName()));
		
		rms = mRecordList.get(position).GetRecordtime();
		rs = (long)(rms / 1000);
		rm = (long)((rs / 60) % 60);
		rh = (long)((rs / 60) / 60);
		rs = rs % 60;
		rms = rms % 60;
		
		holder.r_h10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(rh / 10)) + "_g", "drawable", context.getPackageName()));
		holder.r_h1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(rh % 10)) + "_g", "drawable", context.getPackageName()));
		
		holder.r_m10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(rm / 10)) + "_g", "drawable", context.getPackageName()));
		holder.r_m1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(rm % 10)) + "_g", "drawable", context.getPackageName()));
		
		holder.r_s10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(rs / 10)) + "_g", "drawable", context.getPackageName()));
		holder.r_s1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(rs % 10)) + "_g", "drawable", context.getPackageName()));
		
		holder.r_ms10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(rms / 10)) + "_g", "drawable", context.getPackageName()));
		holder.r_ms1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(rms % 10)) + "_g", "drawable", context.getPackageName()));
		
		ems = mRecordList.get(position).GetStudyingtime();
		es = (long)(ems / 1000);
		em = (long)((es / 60) % 60);
		eh = (long)((es / 60) / 60);
		es = es % 60;
		ems = ems % 60;
		
		holder.e_h10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(eh / 10)) + "_g", "drawable", context.getPackageName()));
		holder.e_h1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(eh % 10)) + "_g", "drawable", context.getPackageName()));
		
		holder.e_m10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(em / 10)) + "_g", "drawable", context.getPackageName()));
		holder.e_m1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(em % 10)) + "_g", "drawable", context.getPackageName()));
		
		holder.e_s10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(es / 10)) + "_g", "drawable", context.getPackageName()));
		holder.e_s1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(es % 10)) + "_g", "drawable", context.getPackageName()));

		holder.e_ms10.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(ems / 10)) + "_g", "drawable", context.getPackageName()));
		holder.e_ms1.setImageResource(convertView.getResources().getIdentifier("n" + ((int)(ems % 10)) + "_g", "drawable", context.getPackageName()));
		
		return convertView;
	}
	
	
}
